package com.ccod.mapper;

import com.ccod.mapper.config.MapperRefreshProperties;
import com.ccod.mapper.mapper.core.MyMapperRefresh;
import com.ccod.mapper.mapper.core.chain.CoreServiceProcess;
import com.ccod.mapper.mapper.core.chain.process.RefreshResourceProcess;
import com.ccod.mapper.spring.MapperRefreshFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @author ccod
 * @date 2020-12-06 13:19
 **/
@Configuration
@ConditionalOnClass({MapperRefreshFactoryBean.class, MyMapperRefresh.class,SqlSessionFactory.class})
@ConditionalOnBean({ SqlSessionFactory.class})
@EnableConfigurationProperties(MapperRefreshProperties.class)
public class MapperRefreshAutoConfiguration {

    private static final String DEFAULT_MAPPER_LOCATIONS = "classpath*:mapper/**/*Mapper.xml";

    private MapperRefreshProperties mapperRefreshProperties;

    @Autowired(required = false)
    private SqlSessionFactory sqlSessionFactory;

    @Autowired(required = false)
    private List<CoreServiceProcess> serviceProcessList;

    @Value("${mybatis.mapper-locations ? mybatis.mapper-locations:}")
    private String[] mapperLocation;

    @Value("${mybatis-plus.mapper-locations ?mybatis-plus.mapper-locations:}")
    private String[] mapperLocationPlus;

    public MapperRefreshAutoConfiguration(MapperRefreshProperties mapperRefreshProperties) {
        this.mapperRefreshProperties = mapperRefreshProperties;
    }

    @Bean
    public MapperRefreshFactoryBean myMapperRefresh(){
        String[] mapperLocation =  mapperRefreshProperties.getMapperLocations();
        if(mapperLocation == null || mapperLocation.length < 1){
            mapperLocation = this.mapperLocation;
        }
        if(mapperLocation == null || mapperLocation.length < 1){
            mapperLocation = this.mapperLocationPlus;
        }
        if(mapperLocation == null || mapperLocation.length < 1){
            mapperLocation = new String[]{DEFAULT_MAPPER_LOCATIONS};
        }
        MapperRefreshFactoryBean mapperRefreshFactoryBean = new MapperRefreshFactoryBean();
        mapperRefreshFactoryBean.setSqlSessionFactory(sqlSessionFactory);
        mapperRefreshFactoryBean.setDelaySeconds(mapperRefreshProperties.getDelaySeconds());
        mapperRefreshFactoryBean.setSleepSeconds(mapperRefreshProperties.getSleepSeconds());
        mapperRefreshFactoryBean.setEnabledRefresh(mapperRefreshProperties.isEnabledRefresh());
        mapperRefreshFactoryBean.setMapperLocations(mapperLocation);
        if(CollectionUtils.isEmpty(serviceProcessList)){
            serviceProcessList = Arrays.asList(new RefreshResourceProcess());
        }
        mapperRefreshFactoryBean.setServiceProcessList(serviceProcessList);
        return mapperRefreshFactoryBean;
    }
}
